//
//  CYWithdrawHintProjectView.m
//  cyjf
//
//  Created by tauCross on 2017/12/29.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYWithdrawHintProjectView.h"



@interface CYWithdrawHintProjectView ()

@property(nonatomic, strong)UIView *maskBackgroundView;
@property(nonatomic, strong)UIView *contentView;

@end


@implementation CYWithdrawHintProjectView

- (instancetype)initWithRate:(NSNumber *)rate due:(NSNumber *)due amount:(NSNumber *)amount
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if(self)
    {
        self.maskBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.maskBackgroundView.alpha = 0;
        self.maskBackgroundView.backgroundColor = HEXCOLORA(@"000000", 0.5);
        [self addSubview:self.maskBackgroundView];
        
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 295, 398)];
        self.contentView.centerX = self.width / 2;
        self.contentView.bottom = 0;
        [self addSubview:self.contentView];
        {
            UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 295, 340)];
            cView.backgroundColor = [UIColor whiteColor];
            cView.layer.masksToBounds = YES;
            cView.layer.cornerRadius = 4;
            [self.contentView addSubview:cView];
            {
                UIImageView *icon = [[UIImageView alloc] initWithImage:CYImage(@"pic_tan_raise_cash_wallet")];
                icon.centerX = cView.width / 2;
                icon.top = 36;
                [cView addSubview:icon];
                
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cView.width - 40, 100)];
                label.numberOfLines = 0;
                label.attributedText = ({
                    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
                    NSDictionary *normalAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:14], NSForegroundColorAttributeName : HEXCOLOR(@"444444")};
                    NSDictionary *highlightAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:14], NSForegroundColorAttributeName : APP_DEFAULT_COLOR_RED};
                    [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"财主请留步\n为你推荐" attributes:normalAttributes]];
                    [string appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f%%高息标", rate.doubleValue] attributes:highlightAttributes]];
                    [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"，使用当前提现金额投资，将获得" attributes:normalAttributes]];
                    [string appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@元", @(amount.doubleValue * (rate.doubleValue / 100) * due.integerValue / CY_YEAR_DAY).amountString] attributes:highlightAttributes]];
                    [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"收益！" attributes:normalAttributes]];
                    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
                    paragraphStyle.alignment = NSTextAlignmentCenter;
                    [paragraphStyle setLineSpacing:4];
                    [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
                    string;
                });
                label.centerX = cView.width / 2;
                label.top = icon.bottom + 5;
                [cView addSubview:label];
                
                UIButton *investButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 250, 40)];
                investButton.layer.masksToBounds = YES;
                investButton.layer.cornerRadius = 5;
                investButton.layer.borderColor = APP_DEFAULT_COLOR_RED.CGColor;
                investButton.layer.borderWidth = 1;
                [investButton setTitle:@"立即去投资" forState:UIControlStateNormal];
                [investButton setTitleColor:APP_DEFAULT_COLOR_RED forState:UIControlStateNormal];
                investButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
                investButton.centerX = cView.width / 2;
                investButton.bottom = cView.height - 70;
                [investButton addTarget:self action:@selector(onInvestButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
                [cView addSubview:investButton];
                
                UIButton *withdrawButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 250, 40)];
                withdrawButton.layer.masksToBounds = YES;
                withdrawButton.layer.cornerRadius = 5;
                [withdrawButton setBackgroundImage:[UIImage imageWithColor:APP_DEFAULT_COLOR_RED size:CGSizeMake(1, 1)] forState:UIControlStateNormal];
                [withdrawButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [withdrawButton setTitle:@"确认提现" forState:UIControlStateNormal];
                withdrawButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
                withdrawButton.centerX = cView.width / 2;
                withdrawButton.bottom = cView.height - 20;
                [withdrawButton addTarget:self action:@selector(onWithdrawButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
                [cView addSubview:withdrawButton];
            }
            
            UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            [closeButton setImage:CYImage(@"popup_ad_close") forState:UIControlStateNormal];
            closeButton.centerX = self.contentView.width / 2;
            closeButton.bottom = self.contentView.height;
            [closeButton addTarget:self action:@selector(onCloseButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:closeButton];
        }
    }
    return self;
}

- (void)show
{
    APP_LOCK_USER_INTERACTION
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.maskBackgroundView.alpha = 1;
                         self.contentView.centerY = self.height / 2;
                     }
                     completion:^(BOOL finished) {
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

- (void)close
{
    APP_LOCK_USER_INTERACTION
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.maskBackgroundView.alpha = 0;
                         self.contentView.top = self.height;
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

#pragma mark - Control Events
- (void)onCloseButtonTouchUpInside
{
    [self close];
}

- (void)onInvestButtonTouchUpInside
{
    [self close];
    if(self.investAction)
    {
        self.investAction();
    }
}

- (void)onWithdrawButtonTouchUpInside
{
    [self close];
    if(self.withdrawAction)
    {
        self.withdrawAction();
    }
}

@end
